package service

import (
	"shoping.zozoo.net/dao"
	"shoping.zozoo.net/model"
	"shoping.zozoo.net/params"
	"strconv"
	"time"
)

//定义接口
type OrderInterface interface {
	GetOrderById(int64) (*model.Order, error)
	DelOrderById(int64) (bool, error)
	UpdateOrder() (bool, error)
	InsertOrder(*params.OrderParam) (int64, error)
	GetOrderAll() ([]*model.Order, error)
	GetOrderInfoAll() ([]*dao.OrderProduct, error)
}

type OrderService struct {
	dao dao.OrderInterface
}

func NewOrderService(d dao.OrderInterface) OrderInterface {
	return &OrderService{d}
}

func (o *OrderService) GetOrderById(id int64) (order *model.Order, err error) {
	return o.dao.SelectById(id)
}

func (o *OrderService) DelOrderById(id int64) (ok bool, err error) {
	return o.dao.Del(id)
}

func (o *OrderService) UpdateOrder() (ok bool, err error) {
	var (
		order *model.Order
	)

	order = &model.Order{}
	return o.dao.Update(order)
}

func (o *OrderService) InsertOrder(param *params.OrderParam) (id int64, err error) {
	var (
		order *model.Order
		num int64
	)

	//生成订单号
	num = time.Now().UnixNano()

	//组织订单数据
	order = &model.Order{
		UserId: param.UserId,
		ProductId: param.ProductId,
		OrderNum: strconv.FormatInt(num,10),
		Status: 1,
	}

	return o.dao.InsertOrder(order)
}

func (o *OrderService) GetOrderAll() (list []*model.Order, err error) {
	return o.dao.SelectAll()
}

func (o *OrderService) GetOrderInfoAll() (list []*dao.OrderProduct, err error) {
	return o.dao.SelectAllWithInfo()
}
